package com.kuzlak.cms.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuzlak.cms.bean.MyPage;
import com.kuzlak.cms.module.sys.entity.SysLoginLog;
import com.kuzlak.cms.module.sys.mapper.SysLoginLogMapper;
import com.kuzlak.cms.module.sys.service.SysLoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
* @author hesenjan
* @description 针对表【sys_login_log】的数据库操作Service实现
* @createDate 2024-11-30 23:46:05
*/
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog>
    implements SysLoginLogService{


    @Cacheable(cacheNames = "login", key = "#token", unless = "#result == null")
    @Override
    public SysLoginLog isLogin(String token) {


        QueryWrapper<SysLoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysLoginLog::getToken, token).eq(SysLoginLog::getState, 0);
        SysLoginLog one = this.getOne(queryWrapper);
        return one;
    }

    @Override
    public MyPage findByPage(int pagesize, int page, int state, String startDate, String endDate, String userName, String ip, String address) {
        MyPage page1 = new MyPage();
        QueryWrapper<SysLoginLog> queryWrapper = new QueryWrapper<>();
        //   queryWrapper.lambda().eq(SysLoginLog::getDeleted,0);

        queryWrapper.orderByDesc("ID");


        if (StringUtils.isNotEmpty(userName)) {
           // queryWrapper.lambda().inSql(SysLoginLog::getUserId, PermissionSqlModel.userNameEq(userName));
        }
        if (StringUtils.isNotEmpty(ip)) {
            queryWrapper.lambda().likeRight(SysLoginLog::getIp, ip);
        }
        if (StringUtils.isNotEmpty(address)) {
            queryWrapper.lambda().likeRight(SysLoginLog::getAddress, address);
        }
        if (StringUtils.isNotEmpty(startDate)) {
            queryWrapper.apply(StringUtils.isNotBlank(startDate), "DATE(CREATE_DATE)>=DATE_FORMAT('" + startDate + "','%Y-%m-%d')");
        }

        if (StringUtils.isNotEmpty(endDate)) {
            queryWrapper.apply(StringUtils.isNotBlank(endDate), "DATE(CREATE_DATE)<=DATE_FORMAT('" + endDate + "','%Y-%m-%d')");
        }

        if (state > -1) {
            queryWrapper.lambda().eq(SysLoginLog::getState, state);

        }


        Page<SysLoginLog> page0 = new Page<>(page, pagesize);
        IPage<SysLoginLog> data = this.getBaseMapper().selectPage(page0, queryWrapper);
 return  MyPage.parsePage(data);
    }
    @Override
    public boolean clearToken(Long userId) {
        LambdaUpdateWrapper<SysLoginLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysLoginLog::getUserId, userId).set(SysLoginLog::getState, 1);
        return this.update(updateWrapper);
    }
}








